home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DDJ0992.ARJ / DBINIT.ASM < prev    next >
Assembly Source File  |  1992-06-15  |  13KB  |  358 lines

  1. ;dbinit.asm
  2. ;Debugger initialization routines
  3. ;
  4. .386P
  5. ;---------------------------------------------------------------------------- 
  6. ;Copyright 1991, 1992 ASMicro Co. 
  7. ;7/6/91       Rick Knoblaugh
  8. ;-----------------------------------------------------------------------------
  9.                 include dbequ.inc
  10.                 include dbstruc.inc
  11.                 include dbmac.inc
  12.                 include dbpdat.inc
  13.  
  14. data            segment para public 'data16' use16
  15.  
  16.                 extrn   gdtl:fword, gdtlim:word, gdtadrs:dword
  17.                 extrn   idtl:fword, idtlim:word, idtadrs:dword
  18.                 extrn   DATA_BEG:abs, DATA_END:abs, video_seg:word
  19.  
  20. data            ends
  21.  
  22.  
  23. ;--------------------------------------------------------------
  24. ;EXTERNALS                                                    |
  25. ;--------------------------------------------------------------
  26. isrcode         segment para public 'icode16' use16
  27.                 extrn   int_0:far, int_1:far, int_2:far, int_3:far, int_4:far
  28.                 extrn   int_5:far, int_6:far, int_7:far, except_8:far
  29.                 extrn   except_9:far, except_0ah:far, except_0bh:far
  30.                 extrn   except_0ch:far, except_0dh:far, except_0eh:far
  31.                 extrn   except_0fh:far, int_20h:far, int_21h:far
  32.                 extrn   int_22h:far, int_23h:far, int_24h:far, int_25h:far
  33.                 extrn   int_26h:far, int_27h:far, int_70h:far, int_71h:far
  34.                 extrn   int_72h:far, int_73h:far, int_74h:far, int_75h:far
  35.                 extrn   int_76h:far, int_77h:far
  36. isrcode         ends                               
  37.  
  38.  
  39. zcode            segment para public 'code16' use16
  40.                 assume cs:zcode, ds:data, es:data   
  41.                 .8086
  42. last_code       label   byte
  43. sign_msg1       db      'DB Version 1.0', CR, LF, '$'
  44. sign_msg2       db      'Copyright ASMicro Co. 1991, 1992', CR, LF , CR, LF, '$'
  45. noprot_msg      db      'Can not run if already in protected mode.',BEEP, CR, LF, '$'
  46. not386_msg      db      'Program requires at least an 80386.', BEEP, CR, LF, '$'
  47.  
  48. start           proc    far 
  49.  
  50.                 push    ds              ;save psp seg
  51.                 mov     ax, data
  52.                 mov     es, ax
  53.                 mov     ds, ax
  54.  
  55.                 call    verify_cpu
  56.                 jnc     short start_200       ;continue if 386/486 in real mode
  57.  
  58.                 assume  ds:zcode
  59.                 push    cs
  60.                 pop     ds              ;get segment for messages
  61.  
  62.                 mov     ah, DOS_PRT_STRING
  63.                 int     21h
  64.                 mov     ax, 4c01h
  65.                 int     21h
  66. .386P
  67. start_200:
  68.                 call    init_gdt
  69.                 call    init_tss
  70.  
  71.                 mov     ax, data   
  72.                 mov     ds, ax
  73.                 assume  ds:data   
  74.  
  75.                 cli                     ;no ints until protected mode
  76.  
  77.                 mov     ax, gdt_seg
  78.                 movzx   eax, ax
  79.                 shl     eax,  4
  80.                 mov     gdtadrs, eax
  81.                 mov     gdtlim, (GDT_END-GDT_BEG)-1
  82.  
  83.  
  84.                 mov     ax, idt_seg
  85.                 movzx   eax, ax
  86.                 shl     eax,  4
  87.                 mov     idtadrs, eax
  88.                 mov     idtlim, (IDT_END-IDT_BEG)-1
  89.  
  90.                 call    reprogram_pic
  91.  
  92.                 lgdt    gdtl
  93.                 lidt    idtl
  94.  
  95.                 mov     dx, dbstack3    ;get stack segment
  96.                 mov     bx, sp          ;and pointer
  97.                 mov     eax, cr0
  98.                 or      eax, 1          ;turn on protected mode bit
  99.                 mov     cr0, eax        ;go into protected mode
  100.  
  101. ;
  102. ;jump to clear prefetch queue
  103. ;
  104.                 db      0eah            ;far jump
  105.                 dw      offset zcode:start_400
  106.                 dw      gdt_seg:sel_code
  107. start_400:
  108.                 mov     ax, offset gdt_seg:sel_tss
  109.                 ltr     ax
  110.                 xor     ax, ax
  111.                 lldt    ax              ;null ldt
  112.                 mov     ax, seg data
  113.                 movzx   eax, ax
  114.                 push    eax             ;gs
  115.                 push    eax             ;fs
  116.                 push    eax             ;ds
  117.                 push    eax             ;es
  118.  
  119.                 push    0
  120.                 push    dx              ;stack segment
  121.                 push    0
  122.                 push    bx              ;stack pointer
  123.  
  124.                 push    2               ;VM bit set in upper eflags
  125.                 push    3000h           ;NT=0, IOPL=3, CLI in lower eflags
  126.                 push    0
  127.                 push    seg zcode        ;cs of where to return
  128.                 push    0
  129.                 push    offset zcode:start_500  ;ip of where to return
  130. ;
  131. ;Must ensure that Nested Task bit is not set in eflags.  If it were,
  132. ;processor would attempt to switch to a task via the selector in
  133. ;the TSS backlink field.  Since that field is now zero, an invalid TSS
  134. ;fault would occur.
  135. ;
  136.                 pushf
  137.                 pop     ax
  138.                 btr     ax, nest_taskf
  139.                 push    ax
  140.                 popf
  141.                 iretd
  142.  
  143. start_500:                              ;begin vm86 task here
  144.                 pop     bx              ;get saved psp seg
  145.                 sti                     ;interrupts ok now
  146.  
  147.                 assume ds:zcode
  148.                 push    cs
  149.                 pop     ds
  150.                 mov     dx, offset sign_msg1    
  151.                 mov     ah, DOS_PRT_STRING
  152.                 int     21h
  153.                 mov     dx, offset sign_msg2    
  154.                 mov     ah, DOS_PRT_STRING
  155.                 int     21h
  156.                 mov     dx, zcode + 1    
  157.                 mov     ax, offset last_code
  158.                 shr     ax, 4
  159.                 add     dx, ax
  160.                 sub     dx, bx                  ;paragraphs to keep
  161.                 mov     ax, (DOS_TSR_FUNC SHL 8) ;exit code 0      
  162.                 int     21h
  163.  
  164. start           endp        
  165.  
  166.  
  167.  
  168. reprogram_pic   proc    near
  169.                 in      al, 21h
  170.                 mov     ah, al
  171.                 mov     al, 11h         ;init
  172.                 out     20h, al
  173.                 mov     al, 20h         ;irq0 to int 20h
  174.                 out     21h, al
  175.                 jmp     short $ + 2
  176.                 jmp     short $ + 2
  177.                 mov     al, 4
  178.                 out     21h, al
  179.                 jmp     short $ + 2
  180.                 jmp     short $ + 2
  181.                 mov     al, 1
  182.                 out     21h, al
  183.                 jmp     short $ + 2
  184.                 jmp     short $ + 2
  185.                 mov     al, ah
  186.                 out     21h, al
  187.                 ret
  188. reprogram_pic   endp
  189.  
  190. .8086
  191.  
  192.  
  193. verify_cpu      proc    near
  194.                 xor     ax, ax
  195.                 push    ax
  196.                 popf
  197.                 pushf
  198.                 pop     ax
  199.                 and     ax, 0f000h
  200.                 cmp     ax, 0f000h
  201.                 jz      verify_c800     ;not 386
  202.                 mov     ax, 0f000h
  203.                 push    ax
  204.                 popf
  205.                 pushf
  206.                 pop     ax
  207.                 and     ax, 0f000h
  208.                 jz      verify_c800     ;not 386        
  209.                 mov     dx, offset noprot_msg
  210. .386P
  211.                 smsw    ax              ;get pm flag into carry 
  212.                 rcr     ax, 1
  213.                 jmp     short verify_c999
  214. verify_c800:
  215.                 mov     dx, offset not386_msg
  216.                 stc
  217. verify_c999:
  218.                 ret
  219. verify_cpu      endp
  220.  
  221.  
  222. init_gdt        proc    near
  223.                 mov     ax, gdt_seg
  224.                 mov     ds, ax
  225.                 assume  ds:gdt_seg
  226.  
  227.                 mov     dx, tss_seg
  228.                 movzx   edx, dx                 ;base data segment
  229.                 mov     ecx, (TSS_END - TSS_BEG  ) - 1  ;limit
  230.                 mov     ah, TSS_DESC
  231.                 mov     si, offset sel_tss   
  232.                 call    make_entry
  233.  
  234.                 mov     dx, tss_seg
  235.                 movzx   edx, dx                 ;base data segment
  236.                 mov     ecx, (TSS_END - TSS_BEG  ) - 1  ;limit
  237.                 mov     ah, RW_DATA             ;alias as r/w for editing
  238.                 mov     si, offset sel_tss_alias
  239.                 call    make_entry
  240.  
  241.                 mov     dx, gdt_seg
  242.                 movzx   edx, dx                 ;base data segment
  243.                 mov     ecx, (GDT_END - GDT_BEG  ) - 1  ;limit
  244.                 mov     ah, RW_DATA             ;alias as r/w for editing
  245.                 mov     si, offset sel_gdt_alias
  246.                 call    make_entry
  247.  
  248.                 mov     dx, isrcode
  249.                 movzx   edx, dx                 ;base of isr code segment
  250.                 mov     ecx, 0ffffh             ;max segment size
  251.                 mov     ah, ER_CODE
  252.                 mov     si, offset sel_isrcode 
  253.                 call    make_entry
  254.  
  255.                 mov     dx, zcode
  256.                 movzx   edx, dx                 ;base code segment
  257.                 mov     ecx, 0ffffh             ;max segment size
  258.                 mov     ah, ER_CODE
  259.                 mov     si, offset sel_code 
  260.                 call    make_entry
  261.  
  262.                 xor     edx, edx                ;zero base
  263.                 mov     ecx, 8fffffh            ;page granularity and 4 gig limit
  264.                 mov     ah, RW_DATA
  265.                 mov     si, offset sel_databs    
  266.                 call    make_entry
  267.  
  268.                 mov     dx, dbstack             ;PL0 stack
  269.                 movzx   edx, dx                 ;base stack segment
  270.                 mov     ecx, (STACK_END - STACK_BEG  ) - 1  ;limit
  271.                 mov     ah, RW_DATA
  272.                 mov     si, offset sel_stack 
  273.                 call    make_entry
  274.  
  275.                 mov     dx, dbstack3            ;PL3 stack
  276.                 movzx   edx, dx                 ;base stack segment
  277.                 mov     ecx, (STACK3_END - STACK3_BEG  ) - 1  ;limit
  278.                 mov     ah, RW_DATA
  279.                 mov     si, offset sel_stack3
  280.                 call    make_entry
  281.  
  282.                 mov     dx, data
  283.                 movzx   edx, dx                 ;base data segment
  284.                 mov     cx, DATA_END
  285.                 sub     cx, DATA_BEG
  286.                 dec     cx
  287.                 movzx   ecx, cx
  288.  
  289.                 mov     ah, RW_DATA
  290.                 mov     si, offset sel_data  
  291.                 call    make_entry
  292.  
  293.                 int     11h                     ;equipment check
  294.                 mov     edx, 0b800h             ;color segment
  295.                 and     al, 30h                 ;monitor bits
  296.                 cmp     al, 30h                 ;30h=monochrome
  297.                 jne     init_gdt500
  298.                 mov     edx, 0b000h             ;monochrome segment
  299.  
  300. init_gdt500:
  301.                 mov     es:video_seg, dx        ;save video segment
  302.  
  303.                 mov     ecx, (PAGE_SIZE * 4)  - 1    
  304.                 mov     ah, RW_DATA             
  305.                 mov     si, offset sel_video     
  306.                 call    make_entry
  307.  
  308.                 ret
  309. init_gdt        endp
  310.  
  311. ;--------------------------------------------------------------
  312. ;make_entry - Load a GDT entry from information passed as     |
  313. ;             follows:                                        |
  314. ;                                                             |
  315. ;             ds=gdt segent                                   |
  316. ;             si=offset of gdt entry to load                  |
  317. ;             ah=type | dpl                                   |
  318. ;            ecx=limit (also, bits 23:20 are g, b, 0, and avl)|
  319. ;            edx=base segment (convert it to linear)          |
  320. ;--------------------------------------------------------------
  321. make_entry      proc    near
  322.                 shl     edx, 4                  ;convert seg to linear
  323.                 mov     [si].seg_limit_low, cx 
  324.                 mov     [si].seg_base_low, dx  
  325.                 shr     edx, 16
  326.                 mov     [si].seg_base_mid, dl   
  327.                 mov     [si].seg_type_dpl, ah  
  328.                 shr     ecx, 16                 
  329.                 mov     [si].seg_limit_gran, cl 
  330.                 mov     [si].seg_base_top, dh  
  331.  
  332.                 ret
  333. make_entry      endp
  334.  
  335. ;--------------------------------------------------------------
  336. ;init_tss - Initilize TSS with base of I/O bit map and set    |
  337. ;           appropriate bits in I/O bit map per cmd line.     |
  338. ;                                                             |
  339. ;--------------------------------------------------------------
  340. init_tss        proc    near
  341.                 mov     ax, tss_seg
  342.                 mov     ds, ax
  343.                 assume  ds:tss_seg
  344.                 xor     si, si
  345.                 mov     ax, offset gdt_seg:sel_stack
  346.                 mov     [si].t_ess0, ax
  347.                 mov     ax, offset dbstack:db_sp   
  348.                 movzx   eax, ax
  349.                 mov     [si].t_esp0, eax
  350.                 lea     bx, [si].t_iomap 
  351.                 mov     [si].t_iobase, bx
  352.                 ret
  353. init_tss        endp      
  354.   
  355. zcode            ends
  356.                 end  start
  357.  
  358.